package com.zhaohang.cn.uimarger;

import android.content.Context;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhaohang.cn.utils.Util;
import com.zhaohang.cn.zhaohangapp.R;

import java.util.List;


/**
 * 作者: 赵航
 * 创建时间: 2018/10/11 9:16
 * 描述:
 */
public abstract class UiChengeAuto extends RelativeLayout {


    private View               mLoadView;//加载中
    private View               mErrorView;//加载失败
    private View               mSuccessView;//加载成功的view
    private SmartRefreshLayout mSmartRefreshLayout;

    public UiChengeAuto(Context context) {
        super(context);

        init(context);
    }


    public UiChengeAuto(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public UiChengeAuto(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {

        RelativeLayout.LayoutParams layoutParams = new   RelativeLayout.LayoutParams(  RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);



        //加载成功的界面
        //外部传入
        if (mSuccessView == null) {
            // 把继承该BaseAcitivyt的layoutID放进来 显示
            mSuccessView = getSuccessView();
        }

        addView(mSuccessView,layoutParams);
        //加载中的布局
        if (mLoadView == null) {
            mLoadView = View.inflate(getContext(), R.layout.page_loading, null);
        }
        addView(mLoadView,layoutParams);
        //失败
        if (mErrorView == null) {
            mErrorView = View.inflate(getContext(), R.layout.page_error, null);
            Button button = (Button) mErrorView.findViewById(R.id.btn_reload);
            button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    currentState = LOADSTATE.LOADING;
                    showPager();

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            SystemClock.sleep(500);
                            changeView();
                        }
                    }).start();


                }
            });

            mSmartRefreshLayout=(SmartRefreshLayout) mErrorView.findViewById(R.id.uichengerefrash);

            //下拉刷新
            mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(RefreshLayout refreshLayout) {
                   //传入false表示刷新失败
                    mSmartRefreshLayout.finishRefresh(0);
                    currentState = LOADSTATE.LOADING;
                    showPager();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            SystemClock.sleep(500);
                            changeView();
                        }
                    }).start();

                }
            });
        }
        addView(mErrorView,layoutParams);

        //根据状态显示界面
        showPager();

        //根据数据自动切换UI
        changeView();
    }

    protected abstract View getSuccessView();

    //1.定义三种状态
    public enum LOADSTATE {
        LOADING, ERROR, SUCCESS
    }

    //2. 定义当前的状态
    private LOADSTATE currentState = LOADSTATE.LOADING;//第一状态永远是加载中

    //3. 根据当前的状态去切换页面
    public void showPager() {
        //根据当前的状态去显示页面
        //1. 先了隐藏所有的界面
        mLoadView.setVisibility(GONE);
        mErrorView.setVisibility(GONE);
        mSuccessView.setVisibility(GONE);
        //2. 根据当前的状态去显示当前的页面
        switch (currentState) {
            case LOADING:
                mLoadView.setVisibility(VISIBLE);
                break;
            case ERROR:
                mErrorView.setVisibility(VISIBLE);
                break;
            case SUCCESS:
                mSuccessView.setVisibility(VISIBLE);
                break;
        }
    }

    //
    //根据数据自动切换UI
    public void changeView() {
        //去网络获取数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                //谁用谁传
                Object obj = getNetData();
                //判断数据,切换状态
                currentState = checkData(obj);
                Util.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        //根据状态切换UI
                        showPager();
                    }
                });


            }
        }).start();
    }

    //根据数据自动返回状态
    private LOADSTATE checkData(Object obj) {
        //如果当前的对象为空,那么肯定是错误界面
        if (obj == null) {
            return LOADSTATE.ERROR;
        } else {
            //这里返回就是成功了吗?
            //如果走到这里有两种情况,一个是bean对象,一个集合,
            //如果是集合,那么如果集合里的数据个数为0,那么也是失败的
            if (obj instanceof List) {
                //说明是集合
                List list = (List) obj;
                if (list.size() > 0) {
                    //说明有数据
                    return LOADSTATE.SUCCESS;
                } else {
                    //说明没有数据
                    return LOADSTATE.ERROR;
                }
            } else {
                //不是集合,是对象
                return LOADSTATE.SUCCESS;
            }
        }
    }

    //谁用谁传数据进来
    protected abstract Object getNetData();
}
